import numpy as np
import matplotlib.pyplot as plt
import neurolab as nl

text = np.loadtxt('data.txt')
data = text[:,0:2]
labels = text[:,2:]

plt.figure()
plt.scatter(data[:,0],data[:,1])
plt.xlabel('dim 1')
plt.ylabel('dim 2')
plt.title('input data')

dim1_min,dim1_max = data[:,0].min(),data[:,1].min()
dim2_min,dim2_max = data[:,0].max(),data[:,1].max()
num_output=labels.shape[1]
dim1  = [dim1_min,dim1_max]
dim2 = [dim2_min,dim2_max]
nn = nl.net.newp([dim1,dim2],num_output)
error_progress = nn.train(data,labels,epochs=100,show=20,lr=0.03)

plt.figure()
plt.plot(error_progress)
plt.xlabel('Number of epochs')
plt.ylabel('Training error')
plt.title('Training error progress')
plt.grid()
plt.show()

print('\nTest results:')
datad_test = [[0.5,4.2],[4.7,0.3],[3.6,7.9]]
for item in datad_test:
    print(item,'-->',nn.sim([item])[0])